package cn.iocoder.yudao.module.system.service.holiday;

import cn.iocoder.yudao.framework.common.util.date.FestivalsUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.holiday.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.HolidayDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.holiday.HolidayMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 节假日信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
@Log4j2
public class HolidayServiceImpl implements HolidayService {
    public static Map<LocalDate, HolidayDO> map = new HashMap<>();

    @Resource
    private HolidayMapper holidayMapper;

    @PostConstruct
    public void init() {
        map.clear();

        List<HolidayDO> list = holidayMapper.selectList();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (HolidayDO holiday : list) {
            HolidayDO data = new HolidayDO();
            org.springframework.beans.BeanUtils.copyProperties(holiday, data);
            map.put(holiday.getDate(), data);
        }
    }

    @Override
    public Long createHoliday(HolidaySaveReqVO createReqVO) {
        // 插入
        HolidayDO holiday = BeanUtils.toBean(createReqVO, HolidayDO.class);
        holidayMapper.insert(holiday);
        // 返回
        return holiday.getId();
    }

    @Override
    public void updateHoliday(HolidaySaveReqVO updateReqVO) {
        // 校验存在
        validateHolidayExists(updateReqVO.getId());
        // 更新
        HolidayDO updateObj = BeanUtils.toBean(updateReqVO, HolidayDO.class);
        holidayMapper.updateById(updateObj);
    }

    @Override
    public void deleteHoliday(Long id) {
        // 校验存在
        validateHolidayExists(id);
        // 删除
        holidayMapper.deleteById(id);
    }

    private void validateHolidayExists(Long id) {
        if (holidayMapper.selectById(id) == null) {
            throw exception(HOLIDAY_NOT_FOUND);
        }
    }

    @Override
    public HolidayDO getHoliday(Long id) {
        return holidayMapper.selectById(id);
    }

    @Override
    public PageResult<HolidayDO> getHolidayPage(HolidayPageReqVO pageReqVO) {
        return holidayMapper.selectPage(pageReqVO);
    }

    @Override
    public boolean isWorkDay(LocalDate localDate) {
        HolidayDO holiday = getByDate(localDate);
        if (null != holiday ){
            if(holiday.getHoliday() ==0) {
                return true;
            }else{
                return false;
            }
        }
        return !FestivalsUtil.isWeekend(localDate);
    }

    @Override
    public boolean isAHoliday(LocalDate localDate) {
        HolidayDO holiday = getByDate(localDate);
        // log.info("holiday:::"+JSONObject.toJSONString(holiday));
        if (null != holiday) {
            if(holiday.getHoliday() == 1){
                return true;
            }else{
                return false;
            }

        }
        return FestivalsUtil.isWeekend(localDate);
    }

    @Override
    public HolidayDO getByDate(LocalDate localDate) {
        if (!CollectionUtils.isEmpty(map)) {
            return map.get(localDate);
        }
        return holidayMapper.selectOne(HolidayDO::getDate,localDate);
    }


}